﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;

namespace Triples {
    public partial class BoardControl : UserControl {
        public readonly IsSelectedConverter Converter;
        private Controller _Controller;
        private int ResourceId = 0;

        public Controller Controller {
            get { return _Controller ?? (_Controller = this.DataContext as Controller); }
        }


        public BoardControl() {
            InitializeComponent();
            Converter = new IsSelectedConverter();
        }


        private void CardsControl_ContainerCreated(object sender, EventArgs e) {
            if (sender is PlayableCardControl) {
                PlayableCardControl card = sender as PlayableCardControl;

                card.Name = "cardControl_" + (++ResourceId);
                //card.Loaded += new RoutedEventHandler(CardControl_Loaded);
                //PositionPanel.SetPosition(card, null);

                Binding isSelectedBinding = new System.Windows.Data.Binding("IsSelected");
                isSelectedBinding.Converter = Converter;
                card.SetBinding(PlayableCardControl.BackgroundProperty, isSelectedBinding);

                Binding positionBinding = new Binding("Position");
                card.SetBinding(PositionPanel.PositionProperty, positionBinding);

                Binding isEnabledBinding = new System.Windows.Data.Binding("IsEnabled");
                card.SetBinding(PlayableCardControl.IsEnabledProperty, isEnabledBinding);

                card.IsEnabledChanged += CardControl_IsEnabledChanged;

            }
        }


        void CardControl_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e) {
            PlayableCardControl card = sender as PlayableCardControl;

            if (null != card) {
                if (card.IsEnabled) card.Opacity = 1;
                else card.Opacity = 0.5;
            }
        }


        void CardControl_Loaded(object sender, RoutedEventArgs e) {
            Duration duration = new Duration(TimeSpan.FromMinutes(0.3));
            Storyboard storyboard = new Storyboard();
            DoubleAnimation animation = new DoubleAnimation();

            animation.Duration = duration;
            animation.From = 0.0;
            animation.To = 1.0;

            Storyboard.SetTarget(animation, sender as DependencyObject);
            Storyboard.SetTargetProperty(animation, new PropertyPath("ArrangeProgress"));

            storyboard.Duration = duration;
            storyboard.Children.Add(animation);

            Resources.Add("cardLoadingStoryboard_" + (++ResourceId), storyboard);
            storyboard.Begin();
        }
    }
}
